package com.ipharma.gui;

import java.awt.Frame;
import java.awt.event.MouseEvent;

import javax.swing.JDialog;

import com.ipharma.McdError;
import com.ipharma.config.TimeOut;

/**
 * A Swing-based top level window class.
 * <P>
 * 
 * @author JorgE.H
 */
public class GUIDialog extends JDialog implements Runnable {

   /**
    * Attributes
    */
   TimeOut to;

   Thread timer;

   String title;

   boolean expired = false;

   long timeout;

   /**
    * Constructs a new instance.
    */
   public GUIDialog(Frame pParent, String pTitle, boolean pModal) {

      super(pParent, pModal);
      try {
         title = pTitle;
         jbInit();
         pack();
      } catch (Exception e) {
         McdError.add(e);
      }
   }

   /**
    * Constructs a new non-modal unparented instance with a blank title.
    */
   public GUIDialog() {

      this(null, "", false);
   }

   /**
    * Initializes the state of this instance.
    */
   private void jbInit() throws Exception {

      this.setLocation(10, 85);
      this.setResizable(false);
      this.addMouseMotionListener(new GUIDialog_this_mouseMotionAdapter(this));
   }

   /**
    * Returns dialog state
    * 
    * @param true
    *           if timer reaches zero, false otherwise
    */
   public boolean hasExpired() {

      return expired;
   }

   /**
    * Starts execution
    * 
    * @param pMain
    *           the caller object
    */
   public void start(long pSecs) {

      timeout = pSecs;
      this.startTimer(pSecs);
      this.setVisible(true);
   }

   /**
    * Stops execution, go back to menu
    */
   public void stop() {

      this.stopTimer();
      this.hide();
   }

   /**
    * Timeout operations
    */
   public void timeout() {

      this.expired = true;
      this.stop();
   }

   /**
    * Starts counting
    */
   public void startTimer(long pSecs) {

      to = new TimeOut(pSecs);
      timer = new Thread(this);
      timer.start();
   }

   /**
    * Kicks timer, setting it to initial state
    */
   public void kickTimer() {

      to.set(timeout);
   }

   /**
    * Stops timer execution
    */
   public void stopTimer() {

      to.setValid(false);
   }

   /**
    * Timer thread code
    */
   public void run() {

      long secs = to.get();
      try {
         while (secs > 0 && to.isValid()) {
            timer.sleep(1000000); // a second
            secs = to.get();
            to.set(--secs);
         }
         if (to.isValid()) {
            System.out.println("timer expired!");
            this.timeout();
         }
      } catch (Exception e) {
         McdError.add(e);
      }
   }

   void this_mouseDragged(MouseEvent e) {

      this.setLocation(10, 85);
   }
}

class GUIDialog_this_mouseMotionAdapter extends
      java.awt.event.MouseMotionAdapter {

   GUIDialog adaptee;

   GUIDialog_this_mouseMotionAdapter(GUIDialog adaptee) {

      this.adaptee = adaptee;
   }

   public void mouseDragged(MouseEvent e) {

      adaptee.this_mouseDragged(e);
   }
}
